package com.bw.day03.fg

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.net.mvvm.BaseFragment
import com.bw.day03.R
import com.bw.day03.adapter.GoodsAdapter
import com.bw.day03.adapter.OneAdapter
import com.bw.day03.adapter.TwoAdapter
import com.bw.day03.adapter.TwoChildAdapter
import com.bw.day03.databinding.FragmentCategoryBinding


class CategoryFragment : BaseFragment<FragmentCategoryBinding, CategoryViewModel>() {
    lateinit var oneAdapter: OneAdapter
    lateinit var twoAdapter: TwoAdapter
//    lateinit var goodsAdapter: GoodsAdapter
//    lateinit var twoChildAdapter: TwoChildAdapter
    var flag=false
//    var cid=534
//    var page=1
//    var limit=3
    override fun initView() {
        oneAdapter = OneAdapter()
        binding.rvOne.adapter = oneAdapter
        binding.rvOne.layoutManager = LinearLayoutManager(context)
//        twoChildAdapter  = TwoChildAdapter()
        twoAdapter = TwoAdapter()
        binding.rvTwo.adapter = twoAdapter
        val linearLayoutManager = LinearLayoutManager(context)
        binding.rvTwo.layoutManager = linearLayoutManager

        viewModel.category()
        oneAdapter.setOnItemClickListener { adapter, view, position ->
            oneAdapter.setId(oneAdapter.data[position].id)
            flag=true
            linearLayoutManager.scrollToPositionWithOffset(position,0)
            flag=false
        }
        binding.rvTwo.setOnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->
            if (!flag){
                val position =
                    linearLayoutManager.findFirstVisibleItemPosition()
                if (oneAdapter.selectId!=twoAdapter.data[position].id){
                    oneAdapter.setId(twoAdapter.data[position].id)
                }
            }
        }
    binding.rvTwo.setOnScrollListener(object : RecyclerView.OnScrollListener(){
        override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
            super.onScrolled(recyclerView, dx, dy)
            val position=linearLayoutManager.findFirstVisibleItemPosition()
            if (flag&&oneAdapter.selectId!=twoAdapter.data[position].id){
                oneAdapter.setId(twoAdapter.data[position].id)
            }
        }
    })
//        twoAdapter.setOnItemClickListener { adapter, view, position ->
//            cid=twoAdapter.data[position].id
//            viewModel.categoryGoods(twoAdapter.data[position].id,2,page)
//
//        }
//        twoChildAdapter.setOnItemClickListener { adapter, view, position ->
//            cid=twoChildAdapter.data[position].id
//           // viewModel.categoryGoods(twoChildAdapter.data[position].id,2,page)
//            ToastUtils.showLong(cid)
//        }

//        viewModel.categoryGoods(534,2,page)
//        goodsAdapter= GoodsAdapter()
//        binding.rvThree.adapter=goodsAdapter
//        binding.rvThree.layoutManager=GridLayoutManager(context,2)

    }

    override fun initData() {
        viewModel.cateSuccess.observe(this){
            if(it.code==200){
                oneAdapter.data.clear()
                oneAdapter.data.addAll(it.data)
                oneAdapter.notifyDataSetChanged()
                twoAdapter.data.clear()
                twoAdapter.data.addAll(it.data)
                twoAdapter.notifyDataSetChanged()
            }

        }
//        viewModel.goodsSuccess.observe(this){
//            if (page==1){
//                goodsAdapter.data.clear()
//            }
//            goodsAdapter.data.addAll(it.data.list)
//            goodsAdapter.notifyDataSetChanged()
//        }

    }

    override fun getViewModel(): Class<CategoryViewModel> =  CategoryViewModel::class.java

    override fun getLayoutId(): Int = R.layout.fragment_category
}